﻿@using AutoPartsSearch.Web.Extensions
@model AutoPartsSearch.Web.Areas.Administration.Models.Dictionary.DictionaryModel

@{
    ViewBag.Title = "Add";
    Layout = "~/Areas/Administration/Views/_Layout.cshtml";
}
<h2>Добавить/Редактировать</h2>

@using (Html.BeginForm("Add", "Dictionary"))
{
    @Html.ValidationSummary(false)
    
    @Html.HiddenFor(model => model.Id)
    <fieldset>
        <table id="step1" class="table-form">
            <tbody>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.Type)
                    </td>
                    <td>
                        @if (Model.Id.HasValue)
                        {
                           @Html.DisplayFor(model => model.Type)
                           @Html.HiddenFor(model => model.Type)
                        }else
                        {
                            @Html.EditorFor(model => model.Type)
                        }
                            
                        @Html.ValidationMessageFor(model => model.Type)
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.Ru)
                    </td>
                    <td>
                        @Html.EditorFor(model => model.Ru)
                        @Html.ValidationMessageFor(model => model.Ru)
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.Ro)
                    </td>
                    <td>
                        @Html.EditorFor(model => model.Ro)
                        @Html.ValidationMessageFor(model => model.Ro)
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.En)
                    </td>
                    <td>
                        @Html.EditorFor(model => model.En)
                        @Html.ValidationMessageFor(model => model.En)
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.ParentId)
                    </td>
                    <td>
                        @Html.EmptyDropDownFor(model => model.ParentId)
                        @Html.ValidationMessageFor(model => model.ParentId)
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.AdditionalParentId)
                    </td>
                    <td>
                        @Html.EmptyDropDownFor(model => model.AdditionalParentId)
                        @Html.ValidationMessageFor(model => model.AdditionalParentId)
                    </td>
                </tr>
            </tbody>
        </table>
        <p>
            <input type="submit" value="Сохранить"/>
        </p>
    </fieldset>
}
    <link rel="stylesheet" href="~/Content/themes/base/jquery.ui.all.css" />
    <script type="text/javascript" language="javascript" src="~/Scripts/query.js"> </script>
    <script type="text/javascript">
        $(document).ready(function () {
            var parentId = '@(Model.ParentId.HasValue ? Model.ParentId.Value.ToString() : string.Empty)';
            var additionalParentId = '@(Model.AdditionalParentId.HasValue ? Model.AdditionalParentId.Value.ToString() : string.Empty)';

            $('#ParentId').dictselect('@Url.Action("ByParentType", "Dictionary")' + '?type=' + $('#Type').val(), {
                setDefault: true,
                parentControl: $('#Type'),
                urlFormat: function () {
                    return '@Url.Action("ByParentType", "Dictionary")' + '?type=' + $('#Type').val();
                },
                selectedValue: parentId,
                parentIsLoaded: true,
                format: function (item) {
                    return $("<option></option>")
                            .attr("value", item.Id)
                            .attr("valueid", item.Id)
                            .text(item.Name.Default + "/" + item.ParentId);
                }
            });

            $('#AdditionalParentId').dictselect('@Url.Action("ByAdditionalParentType", "Dictionary")' + '?type=' + $('#Type').val(), {
                setDefault: true,
                parentControl: $('#Type'),
                urlFormat: function () {
                    return '@Url.Action("ByAdditionalParentType", "Dictionary")' + '?type=' + $('#Type').val();
                },
                selectedValue: additionalParentId,
                parentIsLoaded: true
            });
        });
    </script>
